package cn.nexttec.noteplayer.ui.activities

import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.content.res.Resources
import android.graphics.Color
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import cn.nexttec.noteplayer.R
import cn.nexttec.noteplayer.common.NOTE
import cn.nexttec.noteplayer.common.Preference
import cn.nexttec.noteplayer.common.debug
import cn.nexttec.noteplayer.common.gson
import cn.nexttec.noteplayer.databinding.ActivityEditorBinding
import cn.nexttec.noteplayer.models.NoteFile
import cn.nexttec.noteplayer.ui.activities.ui.markdownviewer.MarkdownViewerFragment
import cn.nexttec.noteplayer.ui.widget.TabItem
import cn.nexttec.noteplayer.viewmodels.EditorViewModel
import cn.nexttec.noteplayer.viewmodels.VideoQueryResult
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.io.File
import java.nio.charset.Charset
import javax.inject.Inject

@AndroidEntryPoint
class EditorActivity : AppCompatActivity() {
    private var noteFile: NoteFile? = null
    @Inject
    lateinit var preference: Preference
    private lateinit var _binding: ActivityEditorBinding
    private val binding get() = _binding
    private val viewModel:EditorViewModel by viewModels()
    companion object {

        fun start(context: Context, mode: Int = 0) {
            val intent = Intent(context, EditorActivity::class.java)
            intent.putExtra("mode", mode)
            context.startActivity(intent)
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = ActivityEditorBinding.inflate(layoutInflater)
        setContentView(binding.root)
        window.statusBarColor = ContextCompat.getColor(this, R.color.lt_blue)
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

        binding.btnMore.setOnClickListener {

        }
        binding.btnBack.setOnClickListener {
            this@EditorActivity.finish()
        }

        setupTabs()
        noteFile = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getSerializableExtra("note", NoteFile::class.java)
        } else {
            intent.getSerializableExtra("note") as NoteFile
        }

        noteFile?.let {
            NOTE debug it.gson()

            val markdownFile = File(it.filepath)
            val htmlFile =
                File(markdownFile.parentFile, markdownFile.nameWithoutExtension + ".html")

            // 先加载编辑器文本
            binding.editor.setText(markdownFile.readText(Charset.defaultCharset()))

            // 设置 MarkdownViewer 的资源根目录为 markdown 文件所在文件夹
            binding.markdownViewer.setMarkdownBaseDirWithExport(markdownFile.parentFile, htmlFile)

            // 加载 markdown 文件
            binding.markdownViewer.loadMarkdownFromNoteFolder(markdownFile.parentFile, markdownFile)
        }

        binding.markdownViewer.onFavoriteClicked = { md5: String, pos: Long ->
            viewModel.loadVideo(md5, pos)
        }

        val mode = intent.getIntExtra("mode", 0)
        if (noteFile == null && mode == 1) {
            binding.editor.visibility = View.VISIBLE
            binding.markdownViewer.visibility = View.GONE
        }

        viewModel.queryResult.observe(this){
            if (it is VideoQueryResult.Success){
                PlayerActivity.start(this@EditorActivity, it.video, it.position)
            }
        }
    }

    private fun setupTabs() {
        val tabs = listOf(
            TabItem("Markdown", R.drawable.ic_markdown_icon),
            TabItem("Editor", com.gci.lib_markdown_editor.R.drawable.editor),
        )
        binding.slantView.setTabs(tabs) {
            val isLandscape =
                resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
            if (!isLandscape) {
                if (it == 0) {
                    binding.editor.visibility = View.GONE
                    binding.markdownViewer.visibility = View.VISIBLE
                } else {
                    binding.editor.visibility = View.VISIBLE
                    binding.markdownViewer.visibility = View.GONE
                }
            }
        }
    }
}